<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>放大镜</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        img {
            vertical-align: top;
        }

        .box {
            margin: 100px;
            width: 350px;
            height: 350px;
            border: 1px solid #000000;
            position: relative;
        }

        .box>.small>.mask {
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: rgba(255, 255, 0, .3);
            /* 鼠标样式 移动 */
            cursor: move;
        }

        .box>.big {
            display: none;
            position: absolute;
            top: 0;
            left: 360px;
            width: 400px;
            height: 400px;
            border: 1px solid #000000;
            /* 超出部分隐藏 */
            overflow: hidden;
        }

        .box>.big>.bigImg {
            position: absolute;
            top: 0;
            left: 0;
            width: 1600px;
            height: 1600px;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="small">
            <img src="./imgs/001.jpg" alt="">
            <!-- 遮罩层 -->
            <span class="mask"></span>
        </div>
        <div class="big">
            <img class='bigImg' src="./imgs/0001.jpg" alt="">
        </div>
    </div>
</body>
<script>
    var box = document.querySelector('.box')
    var small = document.querySelector('.small')
    var mask = document.querySelector('.mask')
    var big = document.querySelector('.big')
    var bigImg = document.querySelector('.bigImg')

    // 鼠标经过小盒子，让 遮罩层，和 big 显示
    small.onmouseover = function () {
        console.log('aaa');
        mask.style.display = 'block'
        big.style.display = 'block'
    }
    // 鼠标离开小盒子，让 遮罩层，和 big 隐藏
    small.onmouseout = function () {
        sho.style.display = 'none'
        big.style.display = 'none'
    }

    //  遮罩层移动
    small.onmousemove = function (event) {
        event = event || window.event
        var x = event.clientX - this.offsetParent.offsetLeft - mask.offsetWidth / 2
        var y = event.clientY - this.offsetParent.offsetTop - mask.offsetHeight / 2


        if (x < 0) {
            x = 0
        } else if (x > box.offsetWidth - mask.offsetWidth) {
            x = box.offsetWidth - mask.offsetWidth
        }

        if (y < 0) {
            y = 0
        } else if (y > box.offsetHeight - mask.offsetHeight) {
            y = box.offsetHeight - mask.offsetHeight
        }
        console.log(x);

        mask.style.left = x + 'px'
        mask.style.top = y + 'px'

        bigImg.style.left = - x * (bigImg.offsetWidth / small.offsetWidth) + 'px'
        bigImg.style.top = - y * (bigImg.offsetHeight / small.offsetHeight) + 'px'
    }

</script>

</html>